package com.hdsx.restful.util;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;


public class XmlReader {
	
	private  Document docu;
	
	private  Element root;
	
    public XmlReader(String path)
    {
		SAXBuilder sb = new SAXBuilder(); 
		try
		{
			docu = (Document) sb.build(getClass().getResourceAsStream("WEB-INF/webService.xml"));
		} catch (JDOMException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		};
		root=docu.getRootElement(); 
    }
    public XmlReader(InputStream stream)
    {
    	SAXBuilder sb = new SAXBuilder(); 
    	try 
    	{
			docu = (Document) sb.build(stream);
		} catch (JDOMException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
    	root=docu.getRootElement(); 
    }
    public Element getNode(String node)
    {
    	return root.getChild(node);
    }
    public String getValue(String node)
    {
        return root.getChild(node).getValue();
    }
    public String getAttributeValue(String node, String attributeName)
    {
        return root.getChild(node).getAttributeValue(attributeName);
    }
    @SuppressWarnings("unchecked")
	public List<Element> getElements(){
    	
    	return root.getChildren();
    }
    @SuppressWarnings("unchecked")
	public List<Element> getElements(Element el){
    	return el.getChildren();
    }
    @SuppressWarnings("unchecked")
	public List<Element> getElements(String node){
    	return root.getChildren(node);
    }
    
}
